package com.slkj.shilixiaoyuanapp.ui.common;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;

import com.example.api.InjectParam;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.PeopleEntity;
import com.slkj.shilixiaoyuanapp.entity.parent.AskLeaveRecordEntity;
import com.slkj.shilixiaoyuanapp.entity.parent.SchoolActivitysDetailEntity;
import com.slkj.shilixiaoyuanapp.entity.parent.SingleBodyartspecialtyEntity;
import com.slkj.shilixiaoyuanapp.ui.common.adapter.SendObjAdapter;
import com.slkj.shilixiaoyuanapp.util.PinyingUtils;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.tkk.api.RxEventProcessor;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import butterknife.BindView;
import me.zhouzhuo.zzletterssidebar.ZzLetterSideBar;
import me.zhouzhuo.zzletterssidebar.interf.OnLetterTouchListener;
import me.zhouzhuo.zzletterssidebar.widget.ZzRecyclerView;

import static com.slkj.shilixiaoyuanapp.entity.code.RxEventProcessorTag.CHOSE_SPR_FINSH;

/**
 * 选择审批人
 */
@ActivityInfo(layout = R.layout.activity_chose_people, title = "选择对象", rightHint = "确定")
public class ChosePeopleActivity extends BaseActivity {
    @BindView(R.id.recycer)
    ZzRecyclerView mRecyclerView;
    SendObjAdapter adapter;
    @InjectParam
    ArrayList<PeopleEntity> data;
    @InjectParam
    int code;
    @InjectParam
    String tag;
    @BindView(R.id.tv_dialog)
    TextView tvDialog;
    @BindView(R.id.sidebar)
    ZzLetterSideBar sideBar;
    int position;
    private String letter;

    @Override
    protected void init() {
        ChosePeopleActivity__JumpCenter.bind(this);

        sortData();

        adapter = new SendObjAdapter(this,data);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        mRecyclerView.setAdapter(adapter);
        sideBar.setLetterTouchListener(mRecyclerView, adapter, tvDialog, new OnLetterTouchListener() {
            @Override
            public void onLetterTouch(String letter, int position) {
                ChosePeopleActivity.this.letter = letter;
            }

            @Override
            public void onActionUp() {
                int pos = -1;
                for (int i=0;i<data.size();i++){
                    if (data.get(i).getuId() == -1 && data.get(i).getUName().equals(letter)){
                        pos=i;
                        break;
                    }
                }
                if (pos != -1)
                    ((LinearLayoutManager)mRecyclerView.getLayoutManager()).scrollToPositionWithOffset(pos, 0);
            }
        });
    }

    /**
     * 对数据进行排序分组
     */
    private void sortData() {
        if (data.get(0).getuId() == -1){
            return;
        }
        //按照拼音首字母排序
        Collections.sort(data, (lhs, rhs) -> {
            String s1=PinyingUtils.cn2FirstSpell(lhs.getUName());
            String s2=PinyingUtils.cn2FirstSpell(rhs.getUName());
            return Collator.getInstance(Locale.ENGLISH).compare(s1, s2);
        });
        //排序完毕后插入分组实体
        List<PeopleEntity> newData = new LinkedList<>();
        for (int i = 0; i < data.size(); i++) {
            if (i == 0){
                newData.add(new PeopleEntity(PinyingUtils.cn2FirstSpell(data.get(i).getUName()),-1));
            }else {
                if (!PinyingUtils.cn2FirstSpell(data.get(i).getUName()).equals(PinyingUtils.cn2FirstSpell(data.get(i -1).getUName()))){
                    newData.add(new PeopleEntity(PinyingUtils.cn2FirstSpell(data.get(i).getUName()),-1));
                }
            }
            newData.add(data.get(i));

        }
        data.clear();
        data.addAll(newData);
    }

    @Override
    protected void onRightTitleClick() {
        List<Integer> uids = new ArrayList<>();
        List<PeopleEntity> checkData = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            PeopleEntity item = data.get(i);
            if (item.isCheck){
                checkData.add(item);
                uids.add(item.getUId());
            }
        }
        RxEventProcessor.get().post(tag+ CHOSE_SPR_FINSH, code,getPeoplesInfos(checkData),data,uids);
        finish();
    }

    /**
     * 将人员集合转换成id集合
     * @param data
     * @return
     */
    public static  List<Integer> getIdList( List<PeopleEntity> data){
        List<Integer> uids = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            PeopleEntity item = data.get(i);
            uids.add(item.getUId());
        }
        return uids;
    }

    public static  List<Integer> getIdList1( List<SchoolActivitysDetailEntity.AuditorsBean> data){
        List<Integer> uids = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            SchoolActivitysDetailEntity.AuditorsBean item = data.get(i);
            uids.add(item.getUId());
        }
        return uids;
    }

    public static  List<Integer> getIdList2( List<SingleBodyartspecialtyEntity.AuditorEntity> data){
        List<Integer> uids = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            SingleBodyartspecialtyEntity.AuditorEntity item = data.get(i);
            uids.add(item.getUId());
        }
        return uids;
    }


    public static String getPeoplesInfos( List<PeopleEntity> checkData){
        String infos = "";
        if (checkData == null)
            return infos;
        switch (checkData.size()) {
            case 0:
                break;
            case 1:
                infos = checkData.get(0).getUName();
                break;
            case 2:
                infos = checkData.get(0).getUName() + "," + checkData.get(1).getUName();
                break;
            case 3:
                infos = checkData.get(0).getUName() + "," + checkData.get(1).getUName()+ "," + checkData.get(2).getUName();
                break;
            default:
                infos = checkData.get(0).getUName() + "," + checkData.get(1).getUName() + "," + checkData.get(2).getUName()+ "等" + checkData.size() + "人";
                break;
        }
        return  infos;
    }
    public static String getPeoplesInfos1( List<SingleBodyartspecialtyEntity.AuditorEntity> checkData){
        String infos = "";
        if (checkData == null)
            return infos;
        switch (checkData.size()) {
            case 0:
                break;
            case 1:
                infos = checkData.get(0).getUName();
                break;
            case 2:
                infos = checkData.get(0).getUName() + "," + checkData.get(1).getUName();
                break;
            case 3:
                infos = checkData.get(0).getUName() + "," + checkData.get(1).getUName()+ "," + checkData.get(2).getUName();
                break;
            default:
                infos = checkData.get(0).getUName() + "," + checkData.get(1).getUName() + "," + checkData.get(2).getUName()+ "等" + checkData.size() + "人";
                break;
        }
        return  infos;
    }

    public static String getPeoplesInfos2( List<AskLeaveRecordEntity.AuditorEntity> checkData){
        String infos = "";
        if (checkData == null)
            return infos;
        switch (checkData.size()) {
            case 0:
                break;
            case 1:
                infos = checkData.get(0).getUName();
                break;
            case 2:
                infos = checkData.get(0).getUName() + "," + checkData.get(1).getUName();
                break;
            case 3:
                infos = checkData.get(0).getUName() + "," + checkData.get(1).getUName()+ "," + checkData.get(2).getUName();
                break;
            default:
                infos = checkData.get(0).getUName() + "," + checkData.get(1).getUName() + "," + checkData.get(2).getUName()+ "等" + checkData.size() + "人";
                break;
        }
        return  infos;
    }

    public static String getPeoplesInfos3( List<SchoolActivitysDetailEntity.AuditorsBean> checkData){
        String infos = "";
        if (checkData == null)
            return infos;
        switch (checkData.size()) {
            case 0:
                break;
            case 1:
                infos = checkData.get(0).getUName();
                break;
            case 2:
                infos = checkData.get(0).getUName() + "," + checkData.get(1).getUName();
                break;
            case 3:
                infos = checkData.get(0).getUName() + "," + checkData.get(1).getUName()+ "," + checkData.get(2).getUName();
                break;
            default:
                infos = checkData.get(0).getUName() + "," + checkData.get(1).getUName() + "," + checkData.get(2).getUName()+ "等" + checkData.size() + "人";
                break;
        }
        return  infos;
    }
}
